package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.DistributorAddressVo;
import com.xbongbong.paas.pojo.vo.DistributorReceiverVo;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.service.PaasFormDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.businessdata.pojo.dto.BusinessDataDTO;
import com.xbongbong.pro.businessdata.pojo.dto.BussinessCustomerListRelyDTO;
import com.xbongbong.pro.businessdata.pojo.dto.DistributorAddressDTO;
import com.xbongbong.pro.businessdata.pojo.dto.DistributorReceiverDTO;
import com.xbongbong.pro.businessdata.pojo.dto.OrderReviewDTO;
import com.xbongbong.pro.businessdata.pojo.dto.ProductListDataDTO;
import com.xbongbong.pro.businessdata.pojo.vo.BusinessCustomerListRelyVO;
import com.xbongbong.pro.businessproduct.pojo.dto.BusinessProductBatchListDTO;
import com.xbongbong.pro.businessproduct.pojo.dto.BussinessProductBatchAutoMatchDTO;
import com.xbongbong.pro.businessproduct.pojo.vo.BusinessProductBatchListVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.decisiontree.pojo.dto.ContactSearchListDTO;
import com.xbongbong.pro.defaultData.pojo.dto.DefaultDataDTO;
import com.xbongbong.pro.defaultData.pojo.vo.DefaultDataVO;
import com.xbongbong.pro.detailtab.dto.OrderOnPinDTO;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.weblist.pojo.vo.WaitPurchaseDetailVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.BusinessProductService;
import com.xbongbong.saas.service.OrderService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/1/22 11:04
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/businessData")
public class MobileBusinessDataController {
    @Resource
    private PaasFormDataService paasFormDataService;
    @Resource
    private BusinessProductService businessProductService;
    @Resource
    private OrderService orderService;
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid BusinessDataDTO businessDataDTO, BindingResult br) {
        XbbResponse<PaasFormDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(businessDataDTO.getSaasMark(), businessDataDTO.getBusinessType(), null, businessDataDTO.getDistributorMark());
                PaasFormDataEsListVO paasFormDataEsListVO = paasFormDataService.getBusinessDataList(businessDataDTO);
                response = new XbbResponse<>(paasFormDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    @RequestMapping(value = "/product/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getProductlist(@RequestBody @Valid ProductListDataDTO productListDataDTO, BindingResult br) {
        XbbResponse<PaasFormDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(productListDataDTO.getSaasMark(), productListDataDTO.getBusinessType(), null, productListDataDTO.getDistributorMark());
                PaasFormDataEsListVO paasFormDataEsListVO = paasFormDataService.getProductListSuper(productListDataDTO);
                response = new XbbResponse<>(paasFormDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/defaultData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String defaultData(@RequestBody @Valid DefaultDataDTO defaultDataDTO, BindingResult br) {
        XbbResponse<DefaultDataVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DefaultDataVO defaultDataVO = paasFormDataService.getDafaultData(defaultDataDTO);
                response = new XbbResponse<>(defaultDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/customerList/getRely", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String businessCustomerListGetRely(@RequestBody @Valid BussinessCustomerListRelyDTO bussinessCustomerListRelyDTO, BindingResult br) {
        XbbResponse<BusinessCustomerListRelyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BusinessCustomerListRelyVO businessCusomerListGetRely = paasFormDataService.businessCusomerListGetRely(bussinessCustomerListRelyDTO);
                response = new XbbResponse<>(businessCusomerListGetRely);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    @RequestMapping(value = "/product/searchList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getProductSearchlist(@RequestBody @Valid ProductListDataDTO productListDataDTO, BindingResult br) {
        XbbResponse<PaasFormDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaasFormDataEsListVO paasFormDataEsListVO = paasFormDataService.getProductSearchList(productListDataDTO);
                response = new XbbResponse<>(paasFormDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/contract/searchList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getContractSearchlist(@RequestBody @Valid ProductListDataDTO productListDataDTO, BindingResult br) {
        XbbResponse<PaasFormDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaasFormDataEsListVO paasFormDataEsListVO = paasFormDataService.getContractSearchlist(productListDataDTO);
                response = new XbbResponse<>(paasFormDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/purchase/searchList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getPurchaseSearchlist(@RequestBody @Valid ProductListDataDTO productListDataDTO, BindingResult br) {
        XbbResponse<PaasFormDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaasFormDataEsListVO paasFormDataEsListVO = paasFormDataService.getPurchaseSearchList(productListDataDTO);
                response = new XbbResponse<>(paasFormDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/distributor/getAddress", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAppDistributorAddress(@RequestBody @Valid DistributorAddressDTO distributorAddressDTO, BindingResult br){
        XbbResponse<DistributorAddressVo> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DistributorAddressVo distributorAddressVo = paasFormDataService.getDistributorAddress(distributorAddressDTO);
                response = new XbbResponse<>(distributorAddressVo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/distributor/getReceiver", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDistributorReceiver(@RequestBody @Valid DistributorReceiverDTO distributorReceiverDTO, BindingResult br){
        XbbResponse<DistributorReceiverVo> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DistributorReceiverVo distributorAddressVo = paasFormDataService.getDistributorReceiver(distributorReceiverDTO);
                response = new XbbResponse<>(distributorAddressVo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/orderReview/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String orderReview(@RequestBody @Valid OrderReviewDTO OrderReviewDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = orderService.orderReview(OrderReviewDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 自动匹配产品批次
     *
     * @param br BindingResult
     * @return String
     */
    @RequestMapping(value = "/batchList/autoMatch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getProductAutoMatchBatchs(@RequestBody @Valid BussinessProductBatchAutoMatchDTO bussinessProductBatchAutoMatchDTO, BindingResult br) {
        XbbResponse<BusinessProductBatchListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BusinessProductBatchListVO businessProductBatchListVO = businessProductService.getProductAutoMatchBatch(bussinessProductBatchAutoMatchDTO);
                response = new XbbResponse<>(businessProductBatchListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 选择产品批次
     *
     * @param br BindingResult
     * @return String
     */
    @RequestMapping(value = "/batchList/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getProductBatchs(@RequestBody @Valid BusinessProductBatchListDTO businessProductBatchListDTO, BindingResult br) {
        XbbResponse<BusinessProductBatchListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BusinessProductBatchListVO businessProductBatchListVO = businessProductService.getBusinessProductBathList(businessProductBatchListDTO);
                response = new XbbResponse<>(businessProductBatchListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /***
     * 联系人查询
     *
     * @param contactSearchListDTO 入参
     * @param: br
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2021-11-30 11:27
     * @since
     * @version
     */
    @RequestMapping(value = "/contact/searchList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getContactSearchList(@RequestBody @Valid ContactSearchListDTO contactSearchListDTO, BindingResult br) {
        XbbResponse<PaasFormDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.CONTACT.getCode(), null, DistributorMarkEnum.OTHER.getCode());
                PaasFormDataEsListVO paasFormDataEsListVO = paasFormDataService.getContactSearchList(contactSearchListDTO);
                response = new XbbResponse<>(paasFormDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/product/sotckSearch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String productStockSearch(@RequestBody @Valid OrderOnPinDTO orderOnPinDTO, BindingResult br) {
        XbbResponse<WaitPurchaseDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WaitPurchaseDetailVO waitPurchaseDetailVO = paasFormDataService.productStockSearch(orderOnPinDTO);
                response = new XbbResponse<>(waitPurchaseDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
